package com.api.chatai.controller;

import com.api.chatai.ThreadLocal.UserContextHolder;
import com.api.chatai.domain.pojo.Result;
import com.api.chatai.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


@RestController
@RequestMapping("/goods")
@Tag(name = "商品相关接口")
@RequiredArgsConstructor
@Slf4j
public  class  GoodsController {
    @Autowired
    private  GoodsService  goodsService;

    @GetMapping
    @Operation(summary = "查询所有商品")
    public Result list() {
        log.info("查询所有商品");
        return Result.success( goodsService.list()) ;
    }

    @Operation(summary = "商品下单,生成订单,跳转支付宝")
    @GetMapping("/buy/{goodsId}")
    public Result buy(@PathVariable Long goodsId) {
        Integer userId = UserContextHolder.getUser();
        log.info("用户下单,用户id={},商品id={}", userId,goodsId);
        String orderId = goodsService.buy(goodsId,userId.longValue());
        HashMap<String, String> map = new HashMap<>();
        map.put("orderId", orderId);
        return Result.success(map);
    }


}